<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" customClass="customWidth" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="意见标题" prop="title">
				<el-input v-model="form.title" placeholder="意见标题" clearable maxlength="80"></el-input>
			</el-form-item>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="意见类型" prop="mailType">
						<sc-select v-model="form.mailType" :params="{code:'leaderMailType'}" :apiObj="$API.system.sysDic.getSysDicByCode" clearable filterable style="width: 100%"></sc-select>
					</el-form-item>
				</el-col>
			</el-row>
			<el-form-item label="意见内容" prop="remark">
				<el-input v-model="form.remark" placeholder="意见内容" type="textarea" clearable></el-input>
			</el-form-item>
				<el-form-item label="公告附件" prop="attachId">
					<sc-upload-file :apiObj="apiObj"  v-model="fileList" :data="{module:'leadermailbox'}" :showFileList="false" @success="addNewFile">
						<el-button type="primary" icon="el-icon-upload">上传附件</el-button>
					</sc-upload-file>
					<attach-menu v-model:attachIds="form.attachId" :roleFlag="4" :preview="true"></attach-menu>
				</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">发 送</el-button>
		</template>
	</el-dialog>

</template>
<script>

import AttachMenu from "@/views/module/attachMenu";

export default {
	emits: ['success', 'closed'],
	components: {
		AttachMenu
	},
	data() {
		return {
			visible: false,
			isSaveing: false,
			isShowMsg:false,
			mode: "add",
			titleMap: {
				add: '发送意见对话框',
				edit: '编辑意见对话框',
			},
			form: {
				recordId:'',
				title: '',
				mailType: '',
				remark:'',
				attachId:'',
			},
			fileList:[],
			apiObj:this.$API.file.attach.uploadFile,
			rules: {
				title: [
					{required: true, message: '标题不能为空', trigger: 'blur'}
				],
				remark: [
					{required: true, message: '意见内容不能为空', trigger: 'blur'}
				]
			}
		}
	},
	mounted() {
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res;
					if (this.mode == "add") {
						res = await this.$API.info.LeaderMailbox.setLeaderMailBox.post(this.form);
					} else if (this.mode == "edit") {
						res = await this.$API.info.LeaderMailbox.updateLeaderMailbox.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		addNewFile(res, file)
		{
			let tArr =[];
			if(this.form.attachId!=undefined&&this.form.attachId!=null&&this.form.attachId!=''){
				tArr = this.form.attachId.split(",");
			}
			tArr.push(res.data.attachId);
			this.form.attachId = tArr.join(",");
		},
		setData(data) {
			this.form.recordId = data.recordId
			this.form.title = data.title
			this.form.mailType = data.mailType
			this.form.remark = data.remark
			this.form.attachId = data.attachId
			this.getAttachList(data.attachId)
		},
	}
}
</script>

<style>
.customWidth {
	width: 60%;
}
</style>
